package com.blog.dao;

import com.blog.pojo.Article;
import com.blog.pojo.Comment;
import com.blog.pojo.NodeKind;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Description:T0D0
 * @Author XieLiKui
 * 博客数据层
 * @Date 2021/10/6
 **/

@Mapper
@Repository("nodeDao")
public interface NodeDao {
    //查询博客文章分类
    @Select("select * from t_kind")
    ArrayList<NodeKind> findNodeKind();

    //通过kind的id查询分类数量
    @Select("select count from t_kind where id =#{id}")
    int findKindCountById(int id);

    /*按分类查询博客文章记录数
     * */
    @Select("<script>" +
            "select count(*) from t_article " +
            "<where>" +
            "<if test='kindName!=\"\"'>"  +
            " id in" +
            "(select a_id from t_kind_article_middle where k_id in " +
            "(select id from t_kind where name = #{kindName})) " +
            "</if>" +
            "</where>" +
            "</script>"
    )
    int findBlogCount(@Param("kindName") String kindName , @Param("sort") String sort);

    /*按分类查询博客文章
    * kindName:查询分类名
    * sort:排序方式
    * searchIndex:分页搜索开始下标
    * */
    @Select("<script>" +
                "select id,title,time,view,approve,src  from t_article " +
                "<where>" +
                    "<if test='kindName!=\"\"'>"  +
                        " id in" +
                        "(select a_id from t_kind_article_middle where k_id in " +
                        "(select id from t_kind where name = #{kindName})) " +
                    "</if>" +
                "</where>" +
                "order by ${sort} desc limit #{searchIndex},35" +
            "</script>"
    )
    List<Article> findBlog(@Param("kindName") String kindName, @Param("sort")
            String sort, @Param("searchIndex") int searchIndex);


    //模糊查询文章
    @Select("select * from t_article where title like CONCAT(CONCAT('%', #{keywords}), '%') or detail like CONCAT(CONCAT('%', #{keywords}), '%')")
    ArrayList<Article> findBlogCountByKeyword(@Param("keywords") String keywords);

    //模糊查询文章
    @Select("select * from t_article where title like CONCAT(CONCAT('%', #{keywords}), '%') or detail like CONCAT(CONCAT('%', #{keywords}), '%') order by ${sort} desc limit #{searchIndex},35")
    ArrayList<Article> findBlogByKeyword(@Param("keywords") String keywords , @Param("sort") String sort , @Param("searchIndex") int searchIndex);

    //通过id查文章
    @Select("select * from t_article where id = #{id}")
    Article findBlogById(int id);

    //通过id查询评论
    @Select("select * from t_article_comment where a_id = #{id}")
    List<Comment> findBlogCommentById(int id);

    //修改文章点赞数
    @Update("update t_article set approve = approve+1 where id =#{id}")
    void updateArticleApprove(int id);

    //修改文章点赞数
    @Update("update t_article_comment set approve = approve+1 where id =#{id}")
    void updateCommentApprove(int id);

    //评论文章
    @Insert("insert into t_article_comment values(null,#{aid},#{comment.time},#{comment.content},0)")
    void insertArticleComment(@Param("aid")int aid,@Param("comment") Comment comment);

    //按ID查询文章类型
    @Select("select name from t_kind where id in(select k_id from t_kind_article_middle where a_id = #{id})")
    List<String> findKindByArticleId(int id);

    //添加文章
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into t_article values(null, #{time},#{title},#{detail},0,0,#{src})")
    void addArticle(Article article);

    //通过类型名查询类型id
    @Select("<script>" +
            "select id from t_kind where name in " +
            "<foreach item='item' index='index' collection='array' open='(' separator=',' close=')'>"+
            "#{item}"+
            "</foreach>"+
            "</script>" )
    List<Integer> findKindsIdByKindName(String[] kinds);

    //添加文章类型和文章中间表
    @Insert("<script>" +
            "insert into t_kind_article_middle values" +
            "<foreach item='item' index='index' collection='kindIds' separator=','>"+
            "(null,#{item},#{aid})"+
            "</foreach>"+
            "</script>")
    void addKindMiddle(@Param("aid") int aid, @Param("kindIds") List<Integer> kindIds);

    //更新类型文章数量
    @Update("update t_kind set count = #{newCount} where id =#{id}")
    void updateKind(@Param("id") int id , @Param("newCount") int newCount);

    //通过id查询浏览数
    @Select("select view from t_article where id = #{id}")
    int findArticleView(int id);

    //通过id修改浏览数
    @Update("update t_article set view = view+1 where id = #{id}")
    void updateArticleView(int id);
}
